package com.ittx.dzt.shein.service.semi;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.ittx.dzt.edi.constant.PlatformMessageConstant;
import com.ittx.dzt.edi.dto.OrderGetDTO;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.dto.StoreConfigDTO;
import com.ittx.dzt.edi.vo.ResponseMessageVO;
import com.ittx.dzt.shein.handler.AbstractBaseSheinSemiApiHandler;
import com.ittx.dzt.sys.response.Result;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 根据售后单号下载售后单
 * <p>
 * cy.returnOrder.download.byOrderId
 * /open-api/return-order/details 退货单详情查询 https://open.sheincorp.com/zh/documents/apidoc/detail/3001282
 * </p>
 *
 * @author wl_code@163.com
 * @version 1.0
 * @date 2025/8/6
 */
@Slf4j
@Service
public class SheinSemiReturnOrderDownloadByIdService extends AbstractBaseSheinSemiApiHandler<OrderGetDTO> {


	@Override
	public String getSheinSemiMethod() {
		return "/open-api/return-order/details";
	}

	@Override
	public String getLocalApiMethod() {
		return PlatformApiConstant.InboundInterface.RETURNORDER_DOWNLOAD_BY_ORDERID;
	}

	@Override
	public Result<?> doProcess(PlatformApiParamDTO platformApiParamDTO, OrderGetDTO orderGetDTO) {
		// 售后单ID
		String[] refundIdArray = StringUtils.split(orderGetDTO.getRefundId(), ",");
		List<Object> list = new ArrayList<>();
		if (refundIdArray != null && refundIdArray.length > 0) {
			for (String refundId : refundIdArray) {
				Result<?> result = this.getRefundById(platformApiParamDTO, refundId);
				if (!result.isSuccess()) {
					return result;
				}
				Object resultData = result.getData();
				// 同步返回
				if (orderGetDTO.getIsAsync() == 0) {
					list.add(resultData);
				} else {
					platformApiParamDTO.setPlatformIdentifierTag(refundId);
					result = this.sendRequestToPlatform(platformApiParamDTO, resultData, true);
					if (!result.isSuccess()) {
						return result;
					}
				}
			}
		}
		// 同步返回
		if (orderGetDTO.getIsAsync() == 0) {
			return Result.success().addData(list).addMsg(platformApiParamDTO.getStoreConfig().getChainUniqueCode());
		}
		return Result.success();
	}


	/**
	 * 根据id获取售后单
	 *
	 * @param platformApiParamDTO 平台参数
	 * @param id                  售后单id
	 * @return
	 */
	public Result<?> getRefundById(PlatformApiParamDTO platformApiParamDTO, String id) {
		return this.getRefundBySDK(platformApiParamDTO, id);
	}

	/**
	 * 执行api请求sdk
	 *
	 * @param platformApiParamDTO 平台参数
	 * @param id                  售后单id
	 * @return 请求结果 resultMap.get(DATA_FIELD)
	 */
	public Result<?> getRefundBySDK(PlatformApiParamDTO platformApiParamDTO, String id) {
		Map<String, Object> params = new HashMap<>();
		params.put("returnOrderNoList", ListUtil.of(id));
		StoreConfigDTO storeConfig = platformApiParamDTO.getStoreConfig();
		String requestId = platformApiParamDTO.getRequestId();
		Result<?> result = this.executeApi(storeConfig, params, requestId);
		if (!result.isSuccess()) {
			return result;
		}
		Map<String, Object> resultMap = (Map<String, Object>) result.getData();
		Map<String, Object> saleData = (Map<String, Object>) resultMap.get(DATA_FIELD);
		if (MapUtil.isEmpty(saleData)) {
			String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0009_MSG, requestId, id);
			log.error(msg);
			return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0009, msg));
		}
		return Result.success().addData(saleData);
	}

}
